// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <gtest/gtest.h>
#include <climits>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cantor/parser/parser.h>

int main(int argc, char *argv[]) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

namespace cantor::parser {
    TEST(test_parser, case_load_data) {
        ///
        {
            cantor::parser::SqlParser parser;
            std::string sql_load =
                    "load data local infile '/home/work/data' replace into table Orders CHARACTER SET 'utf8' fields terminated by '|' enclosed by '\"'"
                    " escaped by '\' lines terminated by 'hahaha' ignore 1 lines (userid, username, app_id) set app_id=app_id+1;";
            parser.parse(sql_load);
            std::cout << parser.syntax_err_str << std::endl;
            ASSERT_EQ(0, parser.error);
            ASSERT_EQ(1, parser.result.size());
            ASSERT_TRUE(typeid(*(parser.result[0])) == typeid(cantor::parser::LoadDataStmt));
            cantor::parser::LoadDataStmt *load_stmt = (cantor::parser::LoadDataStmt *) parser.result[0];
            std::cout << load_stmt->to_string() << std::endl;
            ASSERT_EQ(3, load_stmt->columns.size());
            ASSERT_EQ(1, load_stmt->set_list.size());
            std::cout<<load_stmt->to_json()<<std::endl;
        }
        ///
        {
            cantor::parser::SqlParser parser;
            std::string sql_load =
                    "load data infile '/home/mark/data' replace into table Orders CHARACTER SET 'utf8' fields terminated by ',' enclosed by '\"' "
                    "lines terminated by '\n' ignore 1 lines;";
            parser.parse(sql_load);
            std::cout << parser.syntax_err_str << std::endl;
            ASSERT_EQ(0, parser.error);
            ASSERT_EQ(1, parser.result.size());
            ASSERT_TRUE(typeid(*(parser.result[0])) == typeid(cantor::parser::LoadDataStmt));
            cantor::parser::LoadDataStmt *load_stmt = (cantor::parser::LoadDataStmt *) parser.result[0];
            std::cout << load_stmt->to_string() << std::endl;
            std::cout<<load_stmt->to_json()<<std::endl;
        }
    }
} // namespace cantor::parser
